import java.util.List;
import java.util.LinkedList;
import java.util.Scanner;
import java.lang.IllegalArgumentException;

public class PrimeNumbers {
	
	private Integer upperLimit;
	private List<Integer> primeNumbers;
	
	public PrimeNumbers (int number) throws IllegalArgumentException {
		if (number < 0) throw new IllegalArgumentException("number can not be less than zero");
		upperLimit = new Integer(number);
	}
	
	public PrimeNumbers (String stringNumber) throws NumberFormatException, IllegalArgumentException {
		upperLimit = new Integer(stringNumber);
		if (upperLimit < 0) throw new IllegalArgumentException("number can not be less than zero");
	}
	
	public Integer getUpperLimit() {
		return upperLimit;
	}
	
	public List<Integer> getPrimeNumbers() {
		if (primeNumbers == null) {
		
			primeNumbers = new LinkedList<>();
			
			mainLoop:
			for (int currentNumber = 2; currentNumber <= upperLimit; ++currentNumber) {
				
				for (int divisor = currentNumber - 1; divisor >= 2; --divisor) {
					
					if (currentNumber % divisor == 0) continue mainLoop;
				}
				
				primeNumbers.add(currentNumber);
			}
			
		}
		
		return primeNumbers;
	}

	public static void main(String[] args) throws IllegalArgumentException {
		final String EXIT = "exit";
		final String NEXT = "next";
		
		if (args.length == 0) {
		
			System.out.format("Menu:%n%s - enter the next number%n%s - exit from program%n", NEXT, EXIT);
			
			Scanner input = new Scanner(System.in);
			
			String command;
			
			do {
				System.out.print("Enter the command: ");
			
				command = input.next();
				
				switch(command) {
					case NEXT: 
						System.out.print("Enter number: ");
						int inputedNumber = input.nextInt();
						PrimeNumbers number = new PrimeNumbers(inputedNumber);
						System.out.println(number.getUpperLimit() + " -> " + number.getPrimeNumbers());
						break;
						
					case EXIT: break;
					
					default: 
						System.out.format("\"%s\" is not a command!%n", command);
				}
		
			} while(!command.equals(EXIT));
		}
		else {
			for (String currentArg : args) {
				try {
					PrimeNumbers number = new PrimeNumbers(currentArg); 
				
					System.out.println(number.getUpperLimit() + " -> " + number.getPrimeNumbers());
				}
				catch(NumberFormatException e) {
					System.out.format("\"%s\" is not a number!%n", currentArg);
				}
		
			}
		}
		
	}
}